<template>
  <div id="msbd-app">
    <el-row class="row">
      <el-col :span="12">
        <el-button type="primary" @click="add">新增</el-button>
        <el-button type="warning" @click="checkBoxSX">批量删除</el-button>
      </el-col>
      <el-col :span="12">
        <el-input @blur="iptBlur" placeholder="请输入内容" v-model="text">
          <el-button slot="append" icon="el-icon-search" @click="seach"></el-button>
        </el-input>
      </el-col>
    </el-row>
    <el-table
      :data="list"
      style="width: 100%; margin-top: 10px"
      max-height="700"
      @selection-change="handleSeleveionChange"
      :default-sort="{ prop: 'date', order: 'descending' }"
      @sort-change="sortFun"
    >
      <el-table-column type="selection" width="70"></el-table-column>
      <el-table-column label="序号" type="index" align="center">
        <template slot-scope="scope">
          <b>{{scope.$index+1}}</b>
        </template>
      </el-table-column>
      <el-table-column prop="title" label="标题"></el-table-column>
      <el-table-column prop="type" label="类别"></el-table-column>
      <el-table-column prop="author" label="发布者"></el-table-column>
      <el-table-column prop="date" label="日期" sortable></el-table-column>
      <el-table-column fixed="right" label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="look(scope.row)">详情</el-button>
          <el-button
            type="text"
            size="small"
            @click="updateXG(scope.row)"
          >修改</el-button>
          <el-button type="text" size="small" @click="deleSX(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      layout="total, sizes, prev, pager, next, jumper"
      :page-sizes="[3, 5, 8,10]"
      :pageSize="pageSize"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="changFun"
    ></el-pagination>
    <el-drawer size="80%" title="详细信息" :visible.sync="isShowDraw" :before-close="handleClose">
      <span>
        <el-form ref="form" :model="form" label-width="80px" label-height="500px">
          <el-form-item label="标题">
            <el-input :disabled="see" v-model="form.title"></el-input>
          </el-form-item>
          <el-form-item label="类别" prop="type">
            <el-select :disabled="see" v-model="form.cid" placeholder="请选择类别">
              <el-option
                v-for="(item , index) in classify"
                :key="index"
                :label="item.name"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="时间">
            <el-select :disabled="see" v-model="form.time" placeholder="请选择用时">
              <el-option label="1分钟" value="1分钟"></el-option>
              <el-option label="3分钟" value="3分钟"></el-option>
              <el-option label="5分钟" value="5分钟"></el-option>
              <el-option label="10分钟" value="10分钟"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item  :disabled="see" label="内容">
            <mavonEditor :disabled="see" style="max-height:500px" scrollStyle v-model="form.content" />
          </el-form-item>
          <el-form-item>
            <el-button :disabled="see" type="primary" @click="onSubmit">确认</el-button>
            <el-button @click="isShowDraw = false">取消</el-button>
          </el-form-item>
        </el-form>
      </span>
    </el-drawer>
  </div>
</template>

<script>
import { mapState, mapActions,mapMutations } from "vuex";
import { mavonEditor } from "mavon-editor";
import "mavon-editor/dist/css/index.css";
export default {
  data() {
    return {
      text: "",
      form: {
        author: "situ"
      },
      isShowDraw: false,
      isArr: [],
      see: false
    };
  },
  methods: {
    ...mapActions("msbd", ["getList", "addList",'updateList','deleteList']),
    ...mapMutations('msbd',['SET_PAGE','SET_PAGSIZE']),
    handleSeleveionChange(val) {
      this.isArr = val;
    },
    sortFun(obj) {
      console.log(obj);
    },
    changFun(age) {
      this.SET_PAGE(age);
      this.getList();
    },
    handleSizeChange(val) {
      this.SET_PAGSIZE(val);
      this.getList();
    },

    //提交
    onSubmit() {
      if (this.form.id) {
        this.updateList(this.form);
        this.$message({
          message: "修改成功",
          type: "success"
        });
      } else {
        this.addList(this.form);
        this.$message({
          message: "新增成功",
          type: "success"
        });
      }
      this.isShowDraw = false;
    },

    handleClose(done) {
      this.$confirm("确定要关闭吗？")
        .then(() => {
          done();
          this.see=false
        })
        .catch(() => {});
    },

    // 新增
    add() {
      this.isShowDraw = true;
    },

    // 查看详情
    look(row) {
      this.isShowDraw = true;
      this.see = true;
      this.form = { ...row };
    },
    updateXG(row) {
      this.isShowDraw = true;
      this.form = { ...row };
    },
    deleSX(id) {
        this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(async () => {
          await this.deleteList(id);
          this.$message({
            type: "success",
            message: "删除成功!"
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    checkBoxSX() {
        let arr = [];
      for (let i = 0; i < this.isArr.length; i++) {
        arr.push(this.isArr[i].id);
      }
      this.deleSX(arr.join(","));
    },
    seach() {
        this.getList(this.text);
    },
    iptBlur(){
        if(this.text==''){
            this.getList()
        }
    }
  },
  mounted() {
    this.getList();
  },
  computed: {
    ...mapState("msbd", ["list", "classify",'total','pageSize'])
  },
  components: {
    mavonEditor
  }
};
</script>

<style scoped>
#msbd-app {
  padding: 20px;
}
</style>